home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / MISSING.C < prev    next >
C/C++ Source or Header  |  1992-02-20  |  6KB  |  271 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/missing.c,v 9.29 1992/02/20 16:23:58 jinx Exp $
  4.  
  5. Copyright (c) 1987-1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* This file contains utilities potentially missing from the math library. */
  36.  
  37. #include "ansidecl.h"
  38. #include "config.h"
  39.  
  40. #ifndef HAS_FREXP
  41.  
  42. double
  43. DEFUN (frexp, (value, eptr),
  44.        double value
  45.        AND int * eptr)
  46. {
  47.   register double x = ((value < 0) ? (-value) : value);
  48.   int e = 0;
  49.   if (x >= 1)
  50.     {
  51.       while (1)
  52.     {
  53.       if (x > 2)
  54.         {
  55.           register double xr = (x / 2);
  56.           register double r = 2;
  57.           register int n = 1;
  58.           while (xr >= r)
  59.         {
  60.           /* ((xr == (x / r)) && (xr >= r) && (x >= (r * r))) */
  61.           xr /= r;
  62.           /* ((xr == (x / (r * r))) && (xr >= 1)) */
  63.           r *= r;
  64.           /* ((xr == (x / r)) && (x >= r)) */
  65.           n += n;
  66.         }
  67.           /* ((xr >= 1) && (xr < r)) */
  68.           x = xr;
  69.           e += n;
  70.         }
  71.       else if (x < 2)
  72.         {
  73.           x /= 2;
  74.           e += 1;
  75.           break;
  76.         }
  77.       else
  78.         {
  79.           x /= 4;
  80.           e += 2;
  81.           break;
  82.         }
  83.     }
  84.     }
  85.   else if ((x > 0) && (x < 0.5))
  86.     {
  87.       while (1)
  88.     {
  89.       if (x < 0.25)
  90.         {
  91.           register double xr = (x * 2);
  92.           register double r = 0.5;
  93.           register int n = 1;
  94.           /* ((xr == (x / r)) && (xr < 0.5) && (x < (r / 2))) */
  95.           while (xr < (r / 2))
  96.         {
  97.           /* ((xr < (r / 2)) && (x < ((r * r) / 2))) */
  98.           xr /= r;
  99.           /* ((xr == (x / (r * r))) && (xr < 0.5)) */
  100.           r *= r;
  101.           /* ((xr == (x / r)) && (x < (r / 2))) */
  102.           n += n;
  103.         }
  104.           /* ((xr >= (r / 2)) && (xr < 0.5)) */
  105.           x = xr;
  106.           e -= n;
  107.         }
  108.       else
  109.         {
  110.           x *= 2;
  111.           e -= 1;
  112.           break;
  113.         }
  114.     }
  115.     }
  116.   (*eptr) = e;
  117.   return ((value < 0) ? (-x) : x);
  118. }
  119.  
  120. double
  121. DEFUN (ldexp, (value, exponent),
  122.        double value
  123.        AND int exponent)
  124. {
  125.   register double x = value;
  126.   register int e = exponent;
  127.   register double r = 2;
  128.   if (e > 0)
  129.     {
  130.       if (e == 1)
  131.     return (x * 2);
  132.       while (1)
  133.     {
  134.       if ((e % 2) != 0)
  135.         x *= r;
  136.       e /= 2;
  137.       if (e == 1)
  138.         return (x * r * r);
  139.       r *= r;
  140.     }
  141.     }
  142.   else if (e < 0)
  143.     {
  144.       e = (-e);
  145.       if (e == 1)
  146.     return (x / 2);
  147.       while (1)
  148.     {
  149.       if ((e % 2) != 0)
  150.         x /= r;
  151.       e /= 2;
  152.       if (e == 1)
  153.         return ((x / r) / r);
  154.       r *= r;
  155.     }
  156.     }
  157.   else
  158.     return (x);
  159. }
  160.  
  161. #endif /* not HAS_FREXP */
  162.  
  163. #ifndef HAS_MODF
  164.  
  165. double
  166. DEFUN (modf, (value, iptr),
  167.        double value
  168.        AND double * iptr)
  169. {
  170.   int exponent;
  171.   double significand = (frexp (value, (&exponent)));
  172.   if ((significand == 0) || (exponent <= 0))
  173.     {
  174.       (*iptr) = 0;
  175.       return (value);
  176.     }
  177.   {
  178.     register double s =
  179.       ((((significand < 0) ? (-significand) : significand) * 2) - 1);
  180.     register int e = (exponent - 1);
  181.     register double n = 1;
  182.     while (1)
  183.       {
  184.     if (e == 0)
  185.       break;
  186.     s *= 2;
  187.     e -= 1;
  188.     n *= 2;
  189.     if (s >= 1)
  190.       {
  191.         s -= 1;
  192.         n += 1;
  193.         if (s <= 0)
  194.           {
  195.         /* Multiply n by 2^e */
  196.         register double b = 2;
  197.         if (e == 0)
  198.           break;
  199.         while (1)
  200.           {
  201.             if ((e % 2) == 1)
  202.               {
  203.             n *= b;
  204.             if (e == 1)
  205.               break;
  206.             e -= 1;
  207.               }
  208.             b *= b;
  209.             e /= 2;
  210.           }
  211.         break;
  212.           }
  213.       }
  214.       }
  215.     if (significand < 0)
  216.     {
  217.       (*iptr) = (- n);
  218.       return (- s);
  219.     }
  220.     else
  221.     {
  222.       (*iptr) = n;
  223.       return (s);
  224.     }
  225.   }
  226. }
  227.  
  228. #endif /* not HAS_MODF */
  229.  
  230. #ifndef HAS_FLOOR
  231.  
  232. double
  233. DEFUN (floor, (x), double x)
  234. {
  235.   double iptr;
  236.   double fraction = (modf (x, (&iptr)));
  237.   return ((fraction < 0) ? (iptr - 1) : iptr);
  238. }
  239.  
  240. double
  241. DEFUN (ceil, (x), double x)
  242. {
  243.   double iptr;
  244.   double fraction = (modf (x, (&iptr)));
  245.   return ((fraction > 0) ? (iptr + 1) : iptr);
  246. }
  247.  
  248. #endif /* not HAS_FLOOR */
  249.  
  250. #ifdef DEBUG_MISSING
  251.  
  252. #include <stdio.h>
  253.  
  254. main ()
  255. {
  256.   double input;
  257.   double output;
  258.   int exponent;
  259.   while (1)
  260.     {
  261.       printf ("Number -> ");
  262.       scanf ("%F", (&input));
  263.       output = (frexp (input, (&exponent)));
  264.       printf ("Input = %G; Output = %G; Exponent = %d\n",
  265.           input, output, exponent);
  266.       printf ("Result = %G\n", (ldexp (output, exponent)));
  267.     }
  268. }
  269.  
  270. #endif /* DEBUG_MISSING */
  271.